package ServerRMI;

import java.rmi.RemoteException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import shared.AboutWindow;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;



public class MainWindow extends FrameView
{

    RMIServer server;
    int randomIndex;

    public MainWindow(SingleFrameApplication app)
    {
        super(app);

        initComponents();

    }

    @Action
    public void showAboutBox()
    {
        if (aboutBox == null)
        {
            JFrame mainFrame = Main.getApplication().getMainFrame();
            aboutBox = new AboutWindow(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Main.getApplication().show(aboutBox);
    }
    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableWidget = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        startServerButton = new javax.swing.JButton();
        stopServerButton = new javax.swing.JButton();
        graphicsView = new ServerRMI.MyGraphicsView();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        myRandomNumber = new javax.swing.JLabel();
        decision = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tableWidget.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Name", "Frags", "Deaths", "Received"
            }
        ));
        tableWidget.setName("tableWidget"); // NOI18N
        jScrollPane1.setViewportView(tableWidget);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ServerRMI.Main.class).getContext().getResourceMap(MainWindow.class);
        tableWidget.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tableWidget.columnModel.title0")); // NOI18N
        tableWidget.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tableWidget.columnModel.title1")); // NOI18N
        tableWidget.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tableWidget.columnModel.title2")); // NOI18N
        tableWidget.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tableWidget.columnModel.title3")); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        startServerButton.setText(resourceMap.getString("startServerButton.text")); // NOI18N
        startServerButton.setName("startServerButton"); // NOI18N
        startServerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startServerButtonActionPerformed(evt);
            }
        });

        stopServerButton.setText(resourceMap.getString("stopServerButton.text")); // NOI18N
        stopServerButton.setName("stopServerButton"); // NOI18N
        stopServerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopServerButtonAction(evt);
            }
        });

        graphicsView.setName("graphicsView"); // NOI18N

        javax.swing.GroupLayout graphicsViewLayout = new javax.swing.GroupLayout(graphicsView);
        graphicsView.setLayout(graphicsViewLayout);
        graphicsViewLayout.setHorizontalGroup(
            graphicsViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );
        graphicsViewLayout.setVerticalGroup(
            graphicsViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                notTraitorFunc(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                traitorFunc(evt);
            }
        });

        myRandomNumber.setText(resourceMap.getString("myRandomNumber.text")); // NOI18N
        myRandomNumber.setName("myRandomNumber"); // NOI18N

        decision.setText(resourceMap.getString("decision.text")); // NOI18N
        decision.setName("decision"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(graphicsView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(startServerButton)
                        .addGap(18, 18, 18)
                        .addComponent(stopServerButton))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(jLabel1))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(decision)
                            .addComponent(myRandomNumber))))
                .addGap(189, 189, 189))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(startServerButton)
                            .addComponent(stopServerButton))
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(52, 52, 52)
                        .addComponent(myRandomNumber)
                        .addGap(30, 30, 30)
                        .addComponent(decision))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(graphicsView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ServerRMI.Main.class).getContext().getActionMap(MainWindow.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void startServerButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_startServerButtonActionPerformed
    {//GEN-HEADEREND:event_startServerButtonActionPerformed
        // utworzenie obiektu dostępnego zdalnie
        try
        {
            server = new RMIServer(graphicsView, tableWidget, decision);

            Random generator = new Random();
            randomIndex = generator.nextInt(1000);
            myRandomNumber.setText(myRandomNumber.getText() + randomIndex);
            server.losowy = randomIndex;
        } catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }    
    }//GEN-LAST:event_startServerButtonActionPerformed

    private void stopServerButtonAction(java.awt.event.ActionEvent evt)//GEN-FIRST:event_stopServerButtonAction
    {//GEN-HEADEREND:event_stopServerButtonAction
        server = null;
    }//GEN-LAST:event_stopServerButtonAction

    private void notTraitorFunc(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_notTraitorFunc
        try {
            //server.SendInfoToRestNodes(0, randomIndex, "truthful");
            server.Send(randomIndex, "truthful");
        } catch (RemoteException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_notTraitorFunc

    private void traitorFunc(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_traitorFunc
        try {
            //server.SendInfoToRestNodes(0, randomIndex, "liar");
            server.Send(randomIndex, "liar");
        } catch (RemoteException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_traitorFunc

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel decision;
    private ServerRMI.MyGraphicsView graphicsView;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel myRandomNumber;
    private javax.swing.JButton startServerButton;
    private javax.swing.JButton stopServerButton;
    private javax.swing.JTable tableWidget;
    // End of variables declaration//GEN-END:variables
    private JDialog aboutBox;
}
